package com.kaili.basic.controller.web.v1;


import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.BusinessOperationDeleteDto;
import com.kaili.basic.pojo.dto.WarehousingReturnAddDto;
import com.kaili.basic.pojo.dto.WarehousingReturnQueryDto;
import com.kaili.basic.pojo.entity.WarehousingReturn;
import com.kaili.basic.pojo.entity.WarehousingReturnExport;
import com.kaili.basic.pojo.entity.WarehousingReturnProduct;
import com.kaili.basic.service.WarehousingReturnProductService;
import com.kaili.basic.service.WarehousingReturnService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.ExcelUtil;
import com.kaili.common.util.LoginDetailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 入库退货 前端控制器
 * </p>
 *
 * @author xxx
 * @since 2019-03-07
 */
@RestController
@ApiVersion(1)
@RequestMapping("/{api_version}")
@Api(tags = "入库退货", description = "入库退货相关API")
public class WarehousingReturnController {
    @Autowired
    WarehousingReturnService warehousingReturnService;
    @Autowired
    WarehousingReturnProductService warehousingProductService;

    @PostMapping(value = "/warehousingReturns/addWarehousingReturn")
    @ApiOperation(value = "新增入库退货",notes = "新增入库退货")
    public RestStatus addWarehousingReturn(@RequestBody WarehousingReturnAddDto warehousingReturnAddDto){
        warehousingReturnService.addWarehousingReturn(warehousingReturnAddDto);
        return CommonStatus.ADD_OK;
    }

    @DeleteMapping("/warehousingReturns/deleteWarehousingReturns")
    @ApiOperation(value = "根据主键id删除记录", notes = "根据主键id删除记录")
    public RestStatus deleteWarehousingReturns(@RequestBody BusinessOperationDeleteDto deleteDto) {
        warehousingReturnService.deleteWarehousingReturns(deleteDto);
        return CommonStatus.DELETE_OK;
    }

    @GetMapping(value = "/warehousingReturns/queryWarehousingReturn")
    @ApiOperation(value = "条件查询入库退货信息",notes = "条件查询入库退货信息")
    public Page<WarehousingReturn> queryWarehousingReturn(@ModelAttribute WarehousingReturnQueryDto warehousingReturnQueryDto) {
        return warehousingReturnService.queryWarehousingReturn(new Page<WarehousingReturn>(warehousingReturnQueryDto.getPage(),warehousingReturnQueryDto.getPageSize()),warehousingReturnQueryDto);
    }

    @GetMapping(value = "/warehousingReturns/{returnId}/queryWarehousingReturnById")
    @ApiOperation(value = "根据入库退货主键id查询入库退货信息",notes = "根据入库退货主键id查询入库退货信息")
    public WarehousingReturn queryWarehousingReturnById(@PathVariable("returnId") Long returnId) {
        return warehousingReturnService.queryWarehousingReturnById(returnId);
    }

    @GetMapping(value = "/warehousingReturns/{id}/queryWarehousingReturnProductById")
    @ApiOperation(value = "根据入库退货产品主键id查询入库退货产品信息",notes = "根据入库退货产品主键id查询入库退货产品信息")
    public WarehousingReturnProduct queryWarehousingReturnProductById(@PathVariable("id") Long id) {
        return warehousingProductService.getWarehousingReturnProductById(id);
    }

    @PutMapping(value = "/warehousingReturns/{returnId}/orderConfirmation")
    @ApiOperation(value = "订单确认",notes = "订单确认")
    public RestStatus orderConfirmation(@PathVariable("returnId") Long returnId) {
        WarehousingReturn warehousingReturn = new WarehousingReturn();
        warehousingReturn.setReturnId(returnId);
        warehousingReturn.setOrderStatus(2);
        warehousingReturn.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        warehousingReturnService.updateAffirmOrCancel(warehousingReturn);
        return CommonStatus.ORDER_CONFIRMATION;
    }

    @PutMapping(value = "/warehousingReturns/{returnId}/orderCancellation")
    @ApiOperation(value = "订单取消",notes = "订单取消")
    public RestStatus orderCancellation(@PathVariable("returnId") Long returnId) {
        WarehousingReturn warehousingReturn = new WarehousingReturn();
        warehousingReturn.setReturnId(returnId);
        warehousingReturn.setOrderStatus(3);
        warehousingReturn.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        warehousingReturnService.updateAffirmOrCancel(warehousingReturn);
        return CommonStatus.ORDER_CANCEL;
    }

    @GetMapping("/warehousingReturns/export")
    @ApiOperation(value = "导出入库退货信息")
    public void export(HttpServletResponse response, @ModelAttribute WarehousingReturnQueryDto warehousingReturnQueryDto) throws RestException {
        List<WarehousingReturnExport> list = warehousingReturnService.queryWarehousingReturnExport(warehousingReturnQueryDto);
        ExcelUtil.exportExcel(list,"入库退货信息","sheet1", WarehousingReturnExport.class, UUID.randomUUID().toString()+".xls",response);
    }
}

